/**
 * 
 */
package com.EstateManager.Controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.EstateManager.Dao.EstateBasicInfoDao;
import com.EstateManager.Dao.EstateInfoFileDao;
import com.EstateManager.Dao.LeaseOrSaleDao;
import com.EstateManager.Entity.Estatebasicinfo;
import com.EstateManager.Entity.Estateinfofile;
import com.EstateManager.Entity.Leaseorsaleinfo;
import com.EstateManager.Entity.User;
import com.EstateManager.Utils.CommenUtils;
import com.EstateManager.Utils.FactoryResult;
import com.EstateManager.Utils.Result;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

/**
 * 创建时间 : 上午10:13:30
 * @author Yonggang Liu
 *
 * CommenUtilAction.java
 */

@Controller
@RequestMapping(value = "commenUtil")
public class CommenUtilController {
	
	@Autowired
	private EstateInfoFileDao estateInfoFileDao;
	
	@Autowired
	private EstateBasicInfoDao estateBasicInfoDao;
	
	@Autowired
	private LeaseOrSaleDao leaseOrSaleDao;
	/**
	 * 用户退出登录,注销信息
	 * url : /EstateManager/commenUtil/exit.do
	 * method : get
	 * 
	 * */
	@RequestMapping(value = "exit", method = RequestMethod.GET)
	public void exit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		HttpSession session = request.getSession(false);
		if (session != null) {
			session.removeAttribute("userId");
			session.removeAttribute("userName");
		}
		
		response.sendRedirect("/EstateManager/user/login.do");	
		
		
	}
	
	
	/**
	 * 获取不支持jsonp协议的其他服务器的数据
	 * url : /EstateManager/commenUtil/getJsonFromAntherServer.do
	 * method : get/post
	 * @param url
	 * */
	@RequestMapping(value = "getJsonFromAntherServer")
	@ResponseBody
	public Result getJsonFromAntherServer(String url){
		
		Result result = new Result();
		try {
			result.setCode(1000);
			result.setData(CommenUtils.getJsonString(url));
			result.setMessage("ok");
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			result.setCode(4000);
			result.setData(e);
			result.setMessage("faild");
			e.printStackTrace(System.out);
		}
		return result;
	}
	
	/**
	 * 根据文件类型获取房产描述文件
	 * url : /EstateManager/commenUtil/json-getListFile.do
	 * methoud : post
	 * @param String type
	 * @param int    estateInfoId
	 * */
	@RequestMapping(value = "json-getListFile", method = RequestMethod.POST)
	@ResponseBody
	public Result getListFile(String type, int estateInfoId) {
		Result result;
		try {
			List<Estateinfofile> listFile =  estateInfoFileDao.getListByType(type, estateInfoId);
			result = FactoryResult.getResult(FactoryResult.SUCCESS, listFile, "获取信息成功");
		} catch (Exception e) {
			e.printStackTrace(System.out);
			result = FactoryResult.getResult(FactoryResult.EXCEPTION, e, "获取信息失败");
		}
		return result;
		
		 
	}
	
	/**
	 * 根据用户id返回房产信息数据页
	 * url : /EstateManager/commenUtil/json-getUserEstateList.do
	 * method : post
	 * @param pageSize
	 * @param pageNum
	 * @param userId
	 * 
	 * */
	@RequestMapping(value = "json-getUserEstateList", method = RequestMethod.POST)
	@ResponseBody
	public Result getUserEstateList(int pageSize, int pageNum, Integer userId, HttpServletRequest request){
		Result result;
		if (userId == null) {
			userId = (Integer)request.getSession().getAttribute("userId");
		}
		
		try {
			List<Estatebasicinfo> estaList = estateBasicInfoDao.getListByUserid(pageSize, pageNum, userId);
			for (Estatebasicinfo estatebasicinfo : estaList) {
				User user = (User)estatebasicinfo.getUser().clone();
				user.setUsername("");
				user.setPassword("");;
				estatebasicinfo.setUser(user);
			}
			result = FactoryResult.getResult(FactoryResult.SUCCESS, JSON.toJSONString(estaList, SerializerFeature.DisableCircularReferenceDetect), "获取信息成功");
		} catch (Exception e) {
			e.printStackTrace(System.out);
			result = FactoryResult.getResult(FactoryResult.EXCEPTION, e, "获取信息失败");
		}
		
		
		return result;
	}
	
	/**
	 * 根据用户id，房产审核状态返回房产信息数据页
	 * url : /EstateManager/commenUtil/json-getUserEstateListByType.do
	 * method : post
	 * @param pageSize
	 * @param pageNum
	 * @param userId
	 * @param estateInfoCheckState
	 * */
	@RequestMapping(value = "json-getUserEstateListByType", method = RequestMethod.POST)
	@ResponseBody
	public Result getUserEstateListByType(int pageSize, int pageNum, Integer userId, String estateInfoCheckState, HttpServletRequest request){
		Result result;
		if (userId == null) {
			userId = (Integer)request.getSession().getAttribute("userId");
		}
		
		try {
			List<Estatebasicinfo> estaList = estateBasicInfoDao.getListByUserAndChecktype(pageSize, pageNum, userId, estateInfoCheckState);
			for (Estatebasicinfo estatebasicinfo : estaList) {
				User user = (User)estatebasicinfo.getUser().clone();
				user.setUsername("");
				user.setPassword("");;
				estatebasicinfo.setUser(user);
			}
			result = FactoryResult.getResult(FactoryResult.SUCCESS, JSON.toJSONString(estaList, SerializerFeature.DisableCircularReferenceDetect), "获取信息成功");
		} catch (Exception e) {
			e.printStackTrace(System.out);
			result = FactoryResult.getResult(FactoryResult.EXCEPTION, e, "获取信息失败");
		}
		
		
		return result;
	}
	
	
	/**
	 * 根据用户id，房产审核状态,租赁信息发布状态返回房产信息数据页
	 * url : /EstateManager/commenUtil/json-getUserEstateListByCheckstatePublicstatu.do
	 * method : post
	 * @param pageSize
	 * @param pageNum
	 * @param userId
	 * @param estateInfoCheckState
	 * */
	@RequestMapping(value = "json-getUserEstateListByCheckstatePublicstatu", method = RequestMethod.POST)
	@ResponseBody
	public Result getUserEstateListByCheckstatePublicstatu(int pageSize, int pageNum, Integer userId, String estateInfoCheckState, String publicStatu, HttpServletRequest request){
		Result result;
		if (userId == null) {
			userId = (Integer)request.getSession().getAttribute("userId");
		}
		
		try {
			List<Estatebasicinfo> estaList = estateBasicInfoDao.getEstateListByUserCheckstatePublicstatu(pageSize, pageNum, userId, estateInfoCheckState, publicStatu);
			for (Estatebasicinfo estatebasicinfo : estaList) {
				User user = (User)estatebasicinfo.getUser().clone();
				user.setUsername("");
				user.setPassword("");
				estatebasicinfo.setUser(user);
			}
			result = FactoryResult.getResult(FactoryResult.SUCCESS, JSON.toJSONString(estaList, SerializerFeature.DisableCircularReferenceDetect), "获取信息成功");
		} catch (Exception e) {
			e.printStackTrace(System.out);
			result = FactoryResult.getResult(FactoryResult.EXCEPTION, e, "获取信息失败");
		}
		
		
		return result;
	}
	
	/**
	 * 根据发布信息ID获数据记录
	 * url : /EstateManager/commenUtil/getPublicInfoById.do
	 * method : post
	 * @param id
	 * */
	@RequestMapping(value = "getPublicInfoById", method = RequestMethod.POST)
	@ResponseBody
	public Result getPublicInfoById(Integer id){
		Result result;
		try {
			Leaseorsaleinfo leaseorsaleinfo = leaseOrSaleDao.get(id);
			result = FactoryResult.getResult(FactoryResult.SUCCESS, leaseorsaleinfo, "获取发布信息成功");
		} catch (HibernateException e) {
			e.printStackTrace(System.out);
			result = FactoryResult.getResult(FactoryResult.EXCEPTION, e, "系统异常");
		}
		return result;
		
	}
	
	
	
	
	
	
	
}
